package android.se1;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public  class ForgotPasswordActivity extends Activity {

	private EditText email, securityanswer;
	private Spinner securityquestion;
	private Button changePassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork() // or .detectAll() for all detectable problems
		.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgotpasword);
		email = (EditText) findViewById(R.id.editText2);
		securityquestion = (Spinner) findViewById(R.id.spinner1);
		securityanswer = (EditText) findViewById(R.id.editText3);
		changePassword = (Button) findViewById(R.id.buttonChangePass);

		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

		changePassword.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

				String mavs = "@mavs.uta.edu";
				final String email_address = email.getText().toString();
				int check;
				check = email_address.indexOf(mavs);

				
				if (check == -1) {
					// error.setText("Invalid Email Address! Please Type Valid MAVS Email Address.");

					alertDialog.setTitle("Forgot Password");
					alertDialog
							.setMessage("Invalid Email Address! Please Type Valid MAVS Email Address.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}else {
					postParameters.add(new BasicNameValuePair("email", email.getText().toString()));
					postParameters.add(new BasicNameValuePair("securityquestion", securityquestion.getSelectedItem().toString()));
					postParameters.add(new BasicNameValuePair("securityanswer", securityanswer.getText().toString()));

					String response = null;

					try {

						response = CustomHttpClient.executeHttpPost(
								//"http://192.168.1.145/CSE5324/forgotpassword.php",
								//"http://129.107.149.180/CSE5324/forgotpassword.php",
								"http://omega.uta.edu/~nxk0518/CSE5324/forgotpassword.php",
								//"http://192.168.0.3/mavbulletin/forgotpassword.php",
								postParameters);

						String res = response.toString();

						res = res.replaceAll("\\s+", "");

						if (res.equals("1")) {
							System.out.println("Result =" + res);
							// error.setText("Registration Successful");
							alertDialog.setTitle("Forgot Password");
							alertDialog.setMessage("Information Correct. Please Click Ok to Change Password");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int type) {
											// here you can add functions
											
											SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
											SharedPreferences.Editor editor = sharedPref.edit();
											editor.putString("email",email_address);
											editor.commit();
											Intent resetScreen = new Intent(
													getApplicationContext(),
													ResetPasswordActivity.class);
											startActivity(resetScreen);
										}
									});

							alertDialog.show();

						} else {
							System.out.println("Result =" + res);
							// error.setText("Error in Saving Information!");
							System.out.println("Result =" + res);
							System.out.println("Email =" + email_address);
							System.out.println("securityquestion =" + securityquestion.getSelectedItem().toString());
							System.out.println("securityanswer =" + securityanswer.getText().toString());
							
							Log.e("log_tag",
									"Email =" + email_address);
							alertDialog.setTitle("Forgot Password");
							alertDialog
									.setMessage("Error Information Incorrect!");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int type) {
											// here you can add functions
										}
									});

							alertDialog.show();
						}
					} catch (Exception e) {
						Log.e("log_tag",
								"Error in http connection!!" + e.toString());
						//email.setText(e.toString());
					}

				}

			}

		});

	}
}